﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AdinZ.Web.UI;
using AdinZ.Common;
using System.Text.RegularExpressions;
using System.Data;
/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/4 22:58:06
* */
namespace Web
{
    public partial class sort : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int vID = 0;
            string sID = Request.QueryString["ID"];
            if (sID == null || sID.IndexOf(",", 0) > 0 || AdinZ.Common.DataValidate.IsNumber(sID) == false)
            {
                Response.Write("<H3>栏目ID错误!</H3>");
                Response.End();
            }
            vID = Convert.ToInt32(sID);
            string rPage;
            rPage = Request.QueryString["page"];

            int vPage = 1;
            if (rPage != string.Empty && rPage != null && AdinZ.Common.DataValidate.IsNumber(rPage))
            {
                vPage = Convert.ToInt32(rPage);
            }
            #region 获取页面信息
            string vHtml = "";
            bool vWebPageCache = AdinZ.Common.ConfigHelper.GetConfigBool("WebPageCache"); //判断是否启用了页面缓存
            if (vWebPageCache == false)
            {
                vHtml = Fun_GetChannelContent(vID, vPage);
            }
            else
            {
                int vCacheTime = AdinZ.Common.ConfigHelper.GetConfigInt("CacheTime");
                string channelCacheKey = AdinZ.Common.ConfigHelper.GetConfigString("CacheKey") + "_sort_" + vID + "_" + vPage;
                object channelCacheInfo =AdinZ.Common.DataCache.GetCache(channelCacheKey);

                //判断缓存是否存在
                if (channelCacheInfo == null)
                {
                    vHtml = Fun_GetChannelContent(vID, vPage);
                    AdinZ.Common.DataCache.SetCache(channelCacheKey, (object)vHtml, DateTime.Now.AddSeconds(vCacheTime), TimeSpan.Zero);
                }
                else
                {
                    vHtml = channelCacheInfo.ToString();
                }
            }

            vHtml = Function.Fun_UrlRewriter(vHtml);
            Response.Write(vHtml);
            #endregion

        }



        private string Fun_GetChannelContent(int byID, int byPage)
        {
            //获取当前栏目信息
            Novel.Sys.BLL.novel_sort bll_sort = new Novel.Sys.BLL.novel_sort();
            Novel.Sys.Model.novel_sort model_sort = new Novel.Sys.Model.novel_sort();

            model_sort = bll_sort.GetModel(byID);
            if (model_sort == null)
            {
                Response.Write("<H3>不存在该栏目!</H3>");
                Response.End();
            }

            ////当该页面启用了跳转时,则跳转!
            //if (model_sort.OutSideLink == 1)
            //{
            //    //Response.Redirect(model.Domain);
            //    Response.Clear();
            //    Response.Status = "301 Moved Permanently";
            //    Response.AddHeader("Location", model.Domain);
            //    Response.End();
            //}


            string vTemplateUrl = "sort.html";   //对应的模板
            DataTable dt = bll_sort.GetList("[ID] = " + byID).Tables[0];
            DataRow dr = dt.Rows[0]; 

            Template wt = new Template();
            wt.Load_Template(vTemplateUrl);
            wt.vPage = byPage;
            wt.vCID = byID;

            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitehost}"), siteConfig.SiteHost, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:title}"), siteConfig.WebName, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:seotitle}"), seoConfig.SeoTitle, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:keywords}"), seoConfig.IndexKeywords, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:description}"), seoConfig.IndexDescription, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:filehost}"), siteConfig.FileHost, RegexOptions.IgnoreCase);
            wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitepath}"), Function.Fun_GetSitePath(byID), RegexOptions.IgnoreCase);

            //wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{field:channelkeywords}"), dr["Keywords"].ToString(), RegexOptions.IgnoreCase);
            //wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{field:channeldescription}"), dr["Description"].ToString(), RegexOptions.IgnoreCase);

            wt.vContent = wt.Parser_Tags(0, @"\{field:(.+?)\}", wt.vContent, dr);   //数据信息替换

            //wt.Parser_MyTag();
            wt.Parser_List();
            wt.Parser_Page();
            wt.Parser_IF();
            return wt.vContent;
        }




    }
}